import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/record/points_entity.dart';
import 'package:sports_new/utils/date_time_utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import '../../../resources/colours.dart';
import '../../../resources/styles.dart';
import '../../../utils/routes.dart';
import '../../../utils/utils.dart';
import '../../../widgets/base/qkd_text.dart';
import '../../../widgets/max_bottom_sheet.dart';

class RoadMatchSheet extends StatelessWidget {
  const RoadMatchSheet({super.key, this.info,required this.recordType});
  const RoadMatchSheet.soccer({super.key, this.info}):recordType = 0;
  const RoadMatchSheet.basketball({super.key, this.info}):recordType = 1;

  final ScheduleGroup? info;
  final int recordType;

  @override
  Widget build(BuildContext context) {
    return MaxBottomSheet(
      enableDrag: false,
      backgroundColor: Colors.white,
      shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(10), topRight: Radius.circular(10))),
      onClosing: () {},
      builder: (BuildContext context) => SafeArea(
        child: Container(
          padding: const EdgeInsets.only(left: 16, right: 16),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(height: 15.5),
              Image.asset(
                width: 24,
                Utils.getImgPath("bottomsheet_close.png")
              ).tap(Get.back),
              Container(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Flexible(
                    flex: 1,
                    fit: FlexFit.tight,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Container(
                          alignment: Alignment.center,
                          width: 48,
                          height: 48,
                          decoration:
                              const BoxDecoration(shape: BoxShape.circle),
                          child: CachedNetworkImage(
                              fit: BoxFit.fitWidth,
                              placeholder: (context, url) =>
                                  Styles.placeholderIcon(),
                              errorWidget: (context, url, error) => Image.asset(
                                  Utils.getImgPath("${recordType == 0?"zq":"lq"}_placeholder.png")),
                              imageUrl: info?.leftLogo ?? ''),
                        ),
                        Container(height: 4),
                        QkdText(
                          info?.leftName ?? '',
                          maxLines: 2,
                          textAlign: TextAlign.center,
                        )
                      ],
                    ),
                  ),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      QkdText(info?.roundName ?? "",
                        fontSize: 12,color: Colours.grey66
                      ),
                      info?.leftScore != null
                        ? QkdText(
                            "${info?.leftScore ?? 0}-${info?.rightScore ?? 0}",
                        fontSize: 20,fontWeight: FontWeight.w600)
                        : QkdText("vs", fontSize: 20,fontWeight: FontWeight.w600),
                    ],
                  ),
                  Flexible(
                    flex: 1,
                    fit: FlexFit.tight,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Container(
                          alignment: Alignment.center,
                          width: 48,
                          height: 48,
                          decoration:
                              const BoxDecoration(shape: BoxShape.circle),
                          child: CachedNetworkImage(
                              fit: BoxFit.fitWidth,
                              placeholder: (context, url) =>
                                  Styles.placeholderIcon(),
                              errorWidget: (context, url, error) => Image.asset(
                                  Utils.getImgPath("${recordType == 0?"zq":"lq"}_placeholder.png")),
                              imageUrl: info?.rightLogo ?? ""),
                        ),
                        Container(height: 4),
                        QkdText(
                          info?.rightName ?? '',
                          maxLines: 2,
                          textAlign: TextAlign.center
                        )
                      ],
                    ),
                  )
                ],
              ),
              Container(height: 18),
              Column(
                mainAxisSize: MainAxisSize.min,
                children: List.generate(info?.schedules?.length ?? 0, (index) {
                  var left = info?.leftId;
                  var needChange = recordType == 0?false:true;
                  if (info?.schedules?[index].leftId != left) {
                    needChange = !needChange;
                  }
                  return GestureDetector(
                    onTap: () {
                      if(info?.schedules?[index].matchId != null) {
                        Get.toNamed(
                            recordType == 0?Routes.zqMatchDetail:Routes.lqMatchDetail,
                            arguments: info?.schedules?[index].matchId
                        );
                      }
                    },
                    child: Padding(
                      padding: EdgeInsets.only(
                          top: index != 0 ? 8 : 0, left: 14, right: 14
                      ),
                      child: Container(
                        height: 50,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(4),
                            color: Colours.greyF5F7FA),
                        child: Stack(
                          children: [
                            Row(
                                mainAxisAlignment: MainAxisAlignment.spaceAround,
                                children: [
                                  Expanded(
                                    child: Container(
                                      alignment: Alignment.center,
                                      child: Row(
                                        mainAxisSize: MainAxisSize.min,
                                        children: [
                                          QkdText((needChange && recordType == 0)||(!needChange && recordType == 1)?"${info?.schedules?[index].rightScoreEnd ?? "-"}":"${info?.schedules?[index].leftScoreEnd ?? "-"}",
                                            fontSize: 20,fontWeight: FontWeight.w600,
                                          ),
                                          Container(width: 2),
                                          if(!needChange)
                                          QkdText("(主)",
                                            fontSize: 10,
                                            color: Colours.grey66,
                                          ),
                                          if(needChange)
                                          Container(width: 15),
                                        ]
                                      )
                                    )
                                  ),
                                  Expanded(
                                    child: Container(
                                      alignment: Alignment.center,
                                      child: QkdText("${DateTime.parse(
                                        info?.schedules?[index].matchTime ?? ""
                                      ).formatedString("MM月dd日 hh:mm")}",
                                        fontSize: 12
                                      )
                                    ),
                                  ),
                                  Expanded(
                                    child: Row(
                                      mainAxisSize: MainAxisSize.max,
                                      mainAxisAlignment:
                                      MainAxisAlignment.center,
                                      children: [
                                        QkdText((needChange && recordType == 0)||(!needChange && recordType == 1)?"${info?.schedules?[index].leftScoreEnd ?? "-"}":"${info?.schedules?[index].rightScoreEnd ?? "-"}",
                                          fontSize: 20,fontWeight: FontWeight.w600,
                                        ),
                                        Container(width: 2),
                                        if(needChange)
                                        QkdText("(主)",
                                          fontSize: 10,color: Colours.grey66,
                                        ),
                                        if(!needChange)
                                        Container(width: 15),
                                      ],
                                    ),
                                  )
                                ]
                            ),
                            Align(
                                alignment: Alignment.centerRight,
                                child: Image.asset(
                                    width: 6, height: 10,
                                    Utils.getImgPath("bottomsheet_arrow_right.png")
                                ).paddingOnly(right: 12)
                            )
                          ],
                        ),
                      ),
                    ),
                  );
                }),
              ),
              Container(height: 12)
            ],
          ),
        ),
      ),
    );
  }
}
